package org.grow.took.service;

import org.grow.took.dao.UserInfoDao;
import org.grow.took.entity.RoleInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Service;

import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

/**
 * @Author: xwg
 * @CreateDate: 2021/10/6
 */

@Service
public class PermissionDataSource implements FilterInvocationSecurityMetadataSource {
    @Autowired
    private UserInfoDao userInfoDao;
    @Override
    public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {
//        主战场
        System.out.println(o.getClass());
        FilterInvocation fi = (FilterInvocation)o;
        HttpServletRequest request = fi.getRequest();
        String method = request.getMethod().toUpperCase();
//        localhost:9999/index
        String url = request.getRequestURI();
 /*       select r.*
        from Permission p
        inner join PermissionRole pr on p.permissionId = pr.permissionFk
        inner join RoleInfo r on pr.roleFk = r.roleId
        where p.url='/ceto/player/query/' and p.method='POST'*/
        List<RoleInfo> roles = userInfoDao.queryRolesByUrl(url, method);
        System.out.println(MessageFormat
                .format("从数据库根据 url: {0} 查询到的角色信息： {1}",url,roles));
        List a = roles;
//        Filter
        return a;
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return false;
    }
}
